<?php
/**
 * Author: 孙武 QQ:1228746736
 * Date: 2018/2/11
 * Time: 20:29
 */


/*
 * 错误信息到定义类
 */

namespace app\home\def;
class Err
{
    //错误号
    private $errno;
    //错误信息
    private $errmsg;
    //数据
    private $data;
    //错误号与信息对应数组 偷懒
    private $err;

    /**
     * 定义错误号与错误信息,供系统其他地方通过错误号直接调用
     * Err constructor.
     */
    public function __construct()
    {
        $this->err = [
            '-2' => '尚未登录',
            '-1' => '系统繁忙,请稍后再试',
            '0' => '成功',
            '1' => '成功',//'尚为更改任何数据',
            '2' => '验证码错误',
            '3' => '暂无数据',
            '4' => '类型未定义',
            '5'=>'请支付后重试',
            '10001' => '两次输入的密码不一致',
            '10002' => '当前密码不正确',
            '10003' => '邮箱不存在',

            '11001' => '评论是resguid不能为空',
            '12001' => '该分类已存在',

            '13001' => '金币不足,请补充金币',

        ];
    }


    /**
     * 获得错误信息
     * @param $errno
     * @param string $data
     * @return
     */
    public function info($errno, $data = '')
    {
        if (is_array($data) && empty($data)) {
            $this->errno  = 3;
            $this->errmsg = $this->err["3"];
            return ([
                'errno' => $this->errno,
                'errmsg' => $this->errmsg,
            ]);
        }
        $this->errno  = $errno;
        $this->errmsg = $this->err[$errno];
        $this->data   = $data;
        if ($this->data) {
            return ([
                'errno' => $this->errno,
                'errmsg' => $this->errmsg,
                'data' => $this->data
            ]);
        } else {
            return ([
                'errno' => $this->errno,
                'errmsg' => $this->errmsg,
            ]);
        }
    }

    /**
     * 获得错误信息
     * @param $errno
     * @param string $data
     * @return
     */
    public function infoWithErrmsg($errno, $errmsg, $data = '')
    {
        $this->errno  = $errno;
        $this->errmsg = $errmsg;
        $this->data   = $data;
        if ($this->data) {
            return ([
                'errno' => $this->errno,
                'errmsg' => $this->errmsg,
                'data' => $this->data
            ]);
        } else {
            return ([
                'errno' => $this->errno,
                'errmsg' => $this->errmsg,
            ]);
        }
    }

    /**
     * layui的table请求专用
     */
    public function layuiTable($errno, $count, $data = '')
    {
        $this->errno  = $errno;
        $this->errmsg = $this->err[$errno];
        $this->data   = $data;
        if ($this->data) {
            return ([
                'errno' => $this->errno,
                'errmsg' => $this->errmsg,
                'data' => $this->data,
                'count' => $count
            ]);
        } else {
            return ([
                'errno' => $this->errno,
                'errmsg' => $this->errmsg,
                'count' => $count
            ]);
        }

    }

    /**
     * 统一更新返回
     * $res   model的save更新语句返回的结果
     *使用实例  return $this->err->resUpdata($this->resModel->update(Status::$delete,['PK_ResourceGuid'=>$guid]));
     *
     */
    public function resUpdata($res, $msg = '')
    {
        if ($res > 0) return self::info(0);
        if ($res === 0) return self::info(1);
        return self::info(-1);
    }


    public function resDelete($res){
        if ($res > 0) return self::info(0);
        if ($res === 0) return self::info(1);
        return self::info(-1);
    }


    public function resAdd($res)
    {
        if ($res > 0) return self::info(0);
        if ($res === 0) return self::info(1);
        return self::info(-1);
    }


}